package main

import "github.com/gin-gonic/gin"

type Person struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	r := gin.Default()
	r.GET("/string", retString)
	r.GET("/json", retJson)
	r.GET("/xml", retXml)
	r.GET("/resHeader", resHeader)

	r.Run(":8083")
}

//返回字符串
func retString(c *gin.Context) {
	c.String(200, "加油！")
	c.String(200, "hello, 欢迎%s一起学%s", "你", "go")
}

//返回json
func retJson(c *gin.Context) {
	p := &Person{
		Name:  "Shirdon",
		Email: "shirdonliao@gmail.com",
	}
	c.JSON(200, p)
}

//返回xml
func retXml(c *gin.Context) {
	p := &Person{
		Name:  "Shirdon",
		Email: "shirdonliao@gmail.com",
	}
	c.XML(200, p)
}

//设置响应头
func resHeader(c *gin.Context) {
	c.Header("Content-Type", "text/html;charset=UTF-8")
	c.String(200,
		"<html>"+
			"<head></head>"+
			"<body>"+
			"<h1>Hello</h1>"+
			"</body>"+
			"</html>")
}
